﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.MiniBox.Entity;
using Pub.Class;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// 二次确认请求日志扩展操作类
    /// 
    /// 修改纪录
    ///     2012-07-25 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class DoubleLogExtHelper {
        /// <summary>
        /// 二次确认请求日志扩展缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// 二次确认请求日志扩展添加记录
        /// </summary>
        /// <param name="doubleLogExt">二次确认请求日志扩展实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>返回添加成功后的ID</returns>
        public static Int64 Insert(DoubleLogExt doubleLogExt, string dbkey = "", string[] delCache = null) {
            string strSql = new SQL().Insert(DoubleLogExt._)
                .Value(DoubleLogExt._DoubleID, doubleLogExt.DoubleID)
                .Value(DoubleLogExt._UpResult, doubleLogExt.UpResult)
                .Value(DoubleLogExt._DownResult, doubleLogExt.DownResult)
                .Value(DoubleLogExt._LoginDate, doubleLogExt.LoginDate)
                .ToString();
            strSql += ";select SCOPE_IDENTITY();";
            object objValue = dbkey.IsNullEmpty() ? Data.GetScalar(strSql) : Data.Pool(dbkey).GetScalar(strSql);
            if (objValue.IsAllNull()) return 0;
            Int64 value = objValue.ToBigInt();
            if (delCache.IsNull()) return value;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.DoubleLogExtCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.DoubleLogExtCache_" + s + ")" : s);
            }
            return value;
        }
        /// <summary>
        /// 二次确认请求日志扩展添加记录
        /// </summary>
        /// <param name="doubleLogExt">二次确认请求日志扩展实体类</param>
        /// <returns>返回添加成功后的ID</returns>
        public static Int64 Insert(DoubleLogExt doubleLogExt, string dbkey = "") {
            return Insert(doubleLogExt, dbkey, null);
        }
        /// <summary>
        /// 二次确认请求日志扩展修改记录
        /// </summary>
        /// <param name="doubleLogExt">二次确认请求日志扩展实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(DoubleLogExt doubleLogExt, string dbkey = "", string where = "", string[] delCache = null) {
            if (doubleLogExt.DoubleID.IsNull()) return false;
            string strSql = new SQL().Update(DoubleLogExt._)
                .Set(DoubleLogExt._UpResult, doubleLogExt.UpResult)
                .Set(DoubleLogExt._DownResult, doubleLogExt.DownResult)
                .Set(DoubleLogExt._LoginDate, doubleLogExt.LoginDate)
                .Where("1=1").Where(new Where()
                    .And(DoubleLogExt._DoubleID, doubleLogExt.DoubleID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.DoubleLogExtCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.DoubleLogExtCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 二次确认请求日志扩展修改记录
        /// </summary>
        /// <param name="doubleLogExt">二次确认请求日志扩展实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(DoubleLogExt doubleLogExt, string dbkey = "") {
            return Update(doubleLogExt, dbkey, "", null);
        }
        /// <summary>
        /// 二次确认请求日志扩展修改多条记录
        /// </summary>
        /// <param name="doubleIDList">二次确认请求日志扩展编号列表，用“,”号分隔</param>
        /// <param name="doubleLogExt">二次确认请求日志扩展实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool UpdateByIDList(string doubleIDList,  DoubleLogExt doubleLogExt, string dbkey = "", string where = "", string[] delCache = null) {
            string strSql = new SQL().Update(DoubleLogExt._)
                .Set(DoubleLogExt._UpResult, doubleLogExt.UpResult)
                .Set(DoubleLogExt._DownResult, doubleLogExt.DownResult)
                .Set(DoubleLogExt._LoginDate, doubleLogExt.LoginDate)
                .Where("1=1").Where(new Where()
                    .And(DoubleLogExt._DoubleID, "(" + doubleIDList + ")", Operator.In)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.DoubleLogExtCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.DoubleLogExtCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 二次确认请求日志扩展修改多条记录
        /// </summary>
        /// <param name="doubleIDList">二次确认请求日志扩展编号列表，用“,”号分隔</param>
        /// <param name="doubleLogExt">二次确认请求日志扩展实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool UpdateByIDList(string doubleIDList,  DoubleLogExt doubleLogExt, string dbkey = "") {
            return UpdateByIDList(doubleIDList,  doubleLogExt, dbkey, "", null);
        }
         /// <summary>
        /// 二次确认请求日志扩展删除记录
        /// </summary>
        /// <param name="doubleID">二次确认请求日志扩展编号</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(Int64? doubleID,  string dbkey = "", string where = "", string[] delCache = null) {
            if (doubleID.IsNull()) return false;
            string strSql = new SQL().Delete(DoubleLogExt._)
                .Where("1=1").Where(new Where()
                    .And(DoubleLogExt._DoubleID, doubleID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.DoubleLogExtCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.DoubleLogExtCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 二次确认请求日志扩展删除记录
        /// </summary>
        /// <param name="doubleID">二次确认请求日志扩展编号</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(Int64? doubleID, string dbkey = "") {
            return DeleteByID(doubleID,  dbkey, "", null);
        }
        /// <summary>
        /// 二次确认请求日志扩展删除记录
        /// </summary>
        /// <param name="doubleID">二次确认请求日志扩展编号</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(Int64 doubleID,  string dbkey = "", string where = "", string[] delCache = null) {
            return DeleteByID((Int64?)doubleID,  dbkey, where, delCache);
        }
        /// <summary>
        /// 二次确认请求日志扩展删除记录
        /// </summary>
        /// <param name="doubleID">二次确认请求日志扩展编号</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(Int64 doubleID, string dbkey = "") {
            return DeleteByID((Int64?)doubleID,  dbkey, "", null);
        }
        /// <summary>
        /// 二次确认请求日志扩展删除多条记录
        /// </summary>
        /// <param name="doubleIDList">二次确认请求日志扩展编号列表，用“,”号分隔</param>
        /// <param name="where">删除时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByIDList(string doubleIDList,  string dbkey = "", string where = "", string[] delCache = null) {
            string strSql = new SQL().Delete(DoubleLogExt._)
                .Where("1=1").Where(new Where()
                    .And(DoubleLogExt._DoubleID, "(" + doubleIDList + ")", Operator.In)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.DoubleLogExtCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.DoubleLogExtCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 二次确认请求日志扩展删除多条记录
        /// </summary>
        /// <param name="doubleIDList">二次确认请求日志扩展编号列表，用“,”号分隔</param>
        /// <param name="where">删除时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByIDList(string doubleIDList, string dbkey = "") {
            return DeleteByIDList(doubleIDList,  dbkey, "", null);
        }
        /// <summary>
        /// 二次确认请求日志扩展按主键查询，返回数据的实体类
        /// </summary>
        /// <param name="doubleID">二次确认请求日志扩展编号</param>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <returns>返回单条记录的实体类</returns>
        public static DoubleLogExt SelectByID(Int64 doubleID,  string dbkey = "", string where = "") {
            string cacheNameKey = "GTS.MiniBox.DoubleLogExtCache_SelectByID_{0}".FormatWith(doubleID + "_" +  "_" + where);
            return Cache2.Get<DoubleLogExt>(cacheNameKey, cacheSeconds, () => {
                string strSql = new SQL().From(DoubleLogExt._)
                    .Select(DoubleLogExt._DoubleID)
                    .Select(DoubleLogExt._UpResult)
                    .Select(DoubleLogExt._DownResult)
                    .Select(DoubleLogExt._LoginDate)
                    .Where("1=1").Where(new Where()
                        .And(DoubleLogExt._DoubleID, doubleID, Operator.Equal)
                    ).ToString();
                if (!where.IsNullEmpty()) strSql += where;
                return Data.Pool(dbkey).GetDbDataReader(strSql).ToList<DoubleLogExt>().FirstOrDefault();
            });
        }
        /// <summary>
        /// 二次确认请求日志扩展按主键查询，返回数据的实体类
        /// </summary>
        /// <param name="doubleID">二次确认请求日志扩展编号</param>
        /// <returns>返回单条记录的实体类</returns>
        public static DoubleLogExt SelectByID(Int64 doubleID, string dbkey = "") {
            return SelectByID(doubleID,  dbkey, "");
        }
        /// <summary>
        /// 清除二次确认请求日志扩展按主键查询的缓存
        /// </summary>
        public static void ClearCacheSelectByID(Int64 doubleID,  string where = "") {
            string cacheName = "GTS.MiniBox.DoubleLogExtCache_SelectByID_{0}";
            string cacheNameKey = string.Format(cacheName, doubleID + "_" +  "_" + where);
            Cache2.Remove(cacheNameKey);
        }
        /// <summary>
        /// 二次确认请求日志扩展查询数据，带分页
        /// </summary>
        /// <param name="pageIndex">当前第几页，从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalRecords">返回总记录数</param>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="order">排序字段，不加“order by”</param>
        /// <param name="fieldList">设置需要返回的字段</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <param name="pageEnum">使用哪种分页方式（not_in、max_top、top_top、row_number、mysql、oracle、sqlite）</param>
        /// <returns>返回实体记录集</returns>
        public static IList<DoubleLogExt> SelectPageList(int pageIndex, int pageSize, out int totalRecords, string dbkey = "", string where = "", string order = "", string fieldList = "", PagerSQLEnum pageEnum = PagerSQLEnum.row_number) {
            totalRecords = 0;
            string cacheNameKey = "GTS.MiniBox.DoubleLogExtCache_SelectPageList_{0}_{1}_{2}_{3}_{4}".FormatWith(pageIndex, pageSize, where, order, fieldList);
            string cacheRecordsKey = "GTS.MiniBox.DoubleLogExtCache_RecordsSelectPageList_{0}_{1}_{2}_{3}_{4}".FormatWith(pageIndex, pageSize, where, order, fieldList);
            IList<DoubleLogExt> list = (IList<DoubleLogExt>)Cache2.Get(cacheNameKey);
            if (!list.IsNull()) { totalRecords = (int)Cache2.Get(cacheRecordsKey); return list; }

            using (PagerSQLHelper s = new PagerSQLHelper(pageEnum)) {
                PagerSql sql = s.GetSQL(pageIndex, pageSize, DoubleLogExt._, DoubleLogExt._DoubleID, fieldList.IfNullOrEmpty("[DoubleID],[UpResult],[DownResult],[LoginDate],"), where, "", order);
                DataSet ds = Data.Pool(dbkey).GetDataSet(sql.DataSql + ";" + sql.CountSql);
                if (ds.IsNull()) return list;
                list = ds.Tables[0].ToList<DoubleLogExt>();
                totalRecords = ds.Tables[1].Rows[0][0].ToInt();
                ds.Dispose(); ds = null;
            }
            Cache2.Insert(cacheNameKey, list, cacheSeconds);
            Cache2.Insert(cacheRecordsKey, totalRecords, cacheSeconds);
            return list;
        }
        /// <summary>
        /// 二次确认请求日志扩展查询记录，带分页
        /// </summary>
        /// <param name="pageIndex">当前第几页，从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalRecords">返回总记录数</param>
        /// <returns>返回实体记录集</returns>
        public static IList<DoubleLogExt> SelectPageList(int pageIndex, int pageSize, out int totalRecords, string dbkey = "") {
            return SelectPageList(pageIndex, pageSize, out totalRecords, dbkey, "", "", "", PagerSQLEnum.row_number);
        }
        /// <summary>
        /// 清除二次确认请求日志扩展查询记录，带分页的缓存
        /// </summary>
        public static void ClearCacheSelectPageList() {
            string cacheNameKey = "GTS.MiniBox.DoubleLogExtCache_SelectPageList_(.+?)";
            string cacheRecordsKey = "GTS.MiniBox.DoubleLogExtCache_RecordsSelectPageList_(.+?)";
            Cache2.RemoveByPattern(cacheNameKey);
            Cache2.RemoveByPattern(cacheRecordsKey);
        }
    }
}

